package swd.maintain.project.prggrp_right_maintain;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

      View view=(View)request.getAttribute("view");
      (new WebFunction()).setPortal_to_session(request); //set session from portal
      if (request.getSession().getAttribute("u_id")==null) {
         try{
            request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
            return "login";
         }
         catch(Exception le){
            view.setOnloadScript("can not redirect to login page...please contact IT");
            return "";
         }
      }
      //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
      gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

      DataView DataView=null;
      DataViewForm DataViewForm=null;

      DataView = new DataView(view.getDBAlias(),view.getDBDebug(), view.getSourceCharset(), view.getTargetCharset());
      DataViewForm = new DataViewForm();

      request.setAttribute("DataView",DataView);//讓前端可以呼叫dataview
      request.setAttribute("DataViewForm",DataViewForm);


      Business business=new Business(gqbnDAO);
      //Flow flow=new Flow(gqbnDAO,lstrUserNO,"AFS",view,business);

      //String lstrResult=null;
      String strPrgNoData="";

      try{

         if(view.getprg_no()!=null && view.getprg_no().trim().length()>0){
            view.setSelectShow("");
            strPrgNoData=view.getprg_no().trim();
         } else {
            view.setSelectShow("none");
         }

         view.setButtonVisible("0");

         // Add
         if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
            view.setTipMessage("星號為必填欄位，輸入完資料後按【存檔】");
            view.setAllClear();
            view.setprg_no(strPrgNoData);
            //flow.preAddAction(request.getSession().getAttribute("u_id").toString());
            view.setButtonVisible("1");
            view.setPrgStatus("add");
         }

         // Save
         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("add")){
            //view.setOnloadScript("alert('" + flow.addAction() + "');");
            view.setButtonVisible("2");
            view.setPrgStatus("save");
         }

         //按刪除
         if(view.getPrgAction()!=null && view.getPrgAction().equals("delete")){
            business.exeDelSQL(view,Integer.parseInt(view.getrowsn()));
            view.setrowsn("9999");
         }
         if(view.getPrgAction()!=null && view.getPrgAction().equals("query")){
             view.setTipMessage("欲新增群組，請按上方【新增】鈕後輸入資料按存檔");
         }



         // 離開
         if(view.getPrgAction()!=null && view.getPrgAction().equals("exit")){
             if (view.getPrgStatus()!=null && (view.getPrgStatus().equals("add"))){
                 view.setAllClear();
                 view.setButtonVisible("0");
                 view.setprg_no(strPrgNoData);
                 view.setPrgStatus("exit");
             } else {
                view.setAllClear();
                view.setSelectShow("none");
                view.setPrgStatus("exit");
             }
         }

         // DataView
         business.setDataViewForm(DataViewForm,view,request.getSession().getAttribute("u_id").toString());
         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
